<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div class="app-container">
      <div v-if="selectedRows.length > 0">
            <div>已勾选订单数量合计: {{selectedRows.reduce((acc, row) => acc + row.orderCount, 0)}}</div>
            <div>已勾选请购数量合计: {{selectedRows.reduce((acc, row) => acc + row.pleaseBuyCount, 0)}}</div>
        </div>
      <el-table border v-loading="loading" :data="dataList" ref="table" :max-height="tableMaxHeight"  @selection-change="handleSelectionChange" @sort-change="handleSortChange" :summary-method="totalOutYear"
      show-summary>
      <el-table-column type="selection" width="55" align="center" />
       <!--<el-table-column sortable label="单据号" align="center" prop="nos"/>-->
        <el-table-column sortable label="订单编号" align="center" prop="projectNo"/>
        <el-table-column sortable label="产品编码" align="center" prop="goodsCode"/>
        <el-table-column sortable label="产品名称" align="center" prop="goodsName" show-overflow-tooltip/>
        <el-table-column sortable label="产品型号" align="center" prop="goodModel"/>
        <el-table-column sortable label="产品状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column sortable align="center" label="产品类型" prop="type"/>
        <el-table-column sortable align="center" label="客户名称" prop="customerName"/>
        <el-table-column sortable align="center" label="业务员" prop="salesmanName"/>
        <el-table-column sortable label="计量单位" align="center" prop="unit"/>
        <el-table-column sortable label="需求日期" align="center" prop="deliveryTime"/>
        <el-table-column sortable align="center" label="订单数量" prop="orderCount"/>
        <el-table-column sortable align="center" label="锁定数量" prop="lockCount"/>
        <el-table-column sortable align="center" label="请购数量" prop="pleaseBuyCount"/>
        <el-table-column sortable label="是否自制" align="center" prop="isdo">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.isdo" :value="scope.row.isdo"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark"/>
        <el-table-column label="操作"  align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!--<el-popconfirm :title="`确认删除 ` + scope.row.goodsName + ` 数据项？`" @confirm="handleDelete(scope.row)"
                       style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"  v-if="!scope.row.projectNo  && showTrue == 2">删除产品
              </el-button>
            </el-popconfirm>-->
            <el-popconfirm :title="`确认退回产品吗？`" @confirm="handleSubmitQgd(scope.row)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"  v-if="scope.row.status=='3'" >退回产品</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
      <lvdoBohui-form @ok="getClose" ref="bohuiForm"></lvdoBohui-form>
    </div>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="submitBohui"  v-if="this.status==2">驳回请购</el-button>
      <el-button type="primary" @click="submitSuccess" v-if="this.status==2">同意请购</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {getAction, getById,accAdd,updateAction} from '@/api/manage'
    import LvdoBohuiForm from '@/views/develop/pleaseBuy/prjForGoodsIn/LvdoBohuiForm'
    export default {
        name: "PleaseBuyShenheFormList",
        dicts: ['fund_callback', 'sale_type', 'yes_no', 'isdo','goods_status'],
        components: {
          LvdoBohuiForm,
        },
        data() {
            return {
                title: "请购记录",
                open: false,
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                rows: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                status:null,
                // 销售信息管理表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    mainId: null,
                    isBuy:"1",
                    isdo:"0",
                    params: {},
                },
                url: {
                    list: '/goodsSaledo/goodsSaledo/list',
                    export: '/goodsSale/goodsSale/export',
                    subSuccess: '/goodsSaledo/goodsSaledo/subSuccess/',
                },
                PrjNameOptions: [],
                tableMaxHeight: window.innerHeight - 320,
                selectedRows: [], // 用于存储勾选的行数据
            };
        },
        created() {
            window.addEventListener('resize', this.getTableHeight);
        },
        destroyed() {
            window.removeEventListener('resize', this.getTableHeight)
        },
        methods: {
          handleSubmitQgd(row) {
            const data = row;
            data.status = 39
            updateAction('/goodsSaledo/goodsSaledo/edit', data).then(response => {
              this.$modal.msgSuccess("退回成功");
              this.editOpen = false;
              this.$emit('ok');
            });
          },
            showDoList(row) {
                this.rows = row;
                let that = this;
                this.status = row.status;
                that.queryParams.mainId = row.id;
                that.getList();
                that.open = true;
                that.title="请购记录（ "+row.nos+" ）";
            },
            getClose() {
              this.open = false;
              this.$emit('ok');
            },
            getTableHeight() {
                this.tableHeight = window.innerHeight - 320
            },
            /** 查询销售信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 排序触发事件 */
            handleSortChange(column, prop, order) {
                this.queryParams.orderByColumn = column.prop;
                this.queryParams.isAsc = column.order;
                this.handleQuery();
            },
             // 多选框选中数据
            handleSelectionChange(selection) {
                this.selectedRows = selection;
            },
            totalOutYear(param) {
            const { columns, data } = param;
            const sums = [];

            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "合计";
                    return;
                }
                const values = data.map((item) => Number(item[column.property]));
                // 只合计amount1和amount3，想合并哪些列就在这里指定
                if (column.property === "pleaseBuyCount" || column.property === "orderCount") {

                    sums[index] = values.reduce((prev, curr) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            return accAdd(prev, curr);
                        } else {
                            return prev;
                        }
                    }, 0);
                }
            });
            return sums;
        },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            submitSuccess() {
              this.loading = true;
              getById(this.url.subSuccess, this.queryParams.mainId).then(response => {
                this.$modal.msgSuccess("审批完成！");
                this.open = false;
                this.$emit('ok');
              });
            },
          submitBohui() {
            this.$refs.bohuiForm.showBohui(this.queryParams.mainId,"3");
          },
        }
    };
</script>
